package yuhuibear.poat.object.pipe;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import yuhuibear.poat.object.bean.BeanConstructor;
import yuhuibear.poat.object.bean.PersistentClassLoader;
import yuhuibear.poat.query.ParamArranger;
import yuhuibear.poat.query.SQLBuilder;

/**
 * <p>
 * 数据库的访问管道的基类， 被 @see PipeManager 用于控制结果集缓冲池。
 * </p>
 * 由 @see PipManager 提供实例， 不能被直接new 得到。
 * 
 * @author Administrator
 */
public abstract class ObjectAccessPipeBase implements ObjectAccessPipe {

	@SuppressWarnings("unused")
	private String srodata = "f1a2b4c88f76g8d49e78a1";

	protected static Log log = LogFactory.getLog(PersistentClassLoader.class);

	/**
     *
     */
	protected BeanConstructor bConstructor;

	/**
     *
     */
	protected PersistentClassLoader bAnaliser;

	/**
     *
     */
	protected SQLBuilder sqlp;

	/**
     *
     */
	protected ParamArranger pramSetter;

	/**
     *
     */
	protected ThreadLocal<Connection> conn = new ThreadLocal<Connection>();

	// protected Connection conn;

	/**
	 * 用于分页的偏移量。
	 */
	protected Integer pageNumber;

	/**
	 * 用于分页的页长度。
	 */
	protected Integer pageLength;

	protected boolean distinct = true;

	// /**
	// *
	// * @param con
	// * @param bc
	// * @param ba
	// * @param pa
	// */
	// public ObjectAccessPipeBase(DataSource ds, Integer isolation,
	// BeanConstructor bc, PersistentClassLoader structs, ParamArranger pa)
	// throws Exception {
	// bConstructor = bc;
	// bAnaliser = structs;
	// pramSetter = pa;
	// sqlp = new SQLBuilder(bAnaliser);
	// init(ds.getConnection(), isolation);
	//
	// }

	public ObjectAccessPipeBase(Connection con, Integer isolation,
			BeanConstructor bc, PersistentClassLoader structs, ParamArranger pa)
			throws Exception {
		bConstructor = bc;
		bAnaliser = structs;
		pramSetter = pa;
		sqlp = new SQLBuilder(bAnaliser);
		init(con, isolation);

	}

	private void init(Connection conn, Integer isolation) throws SQLException {

		// if ((new Date()).getTime()
		// - Long.parseLong(srodata.replaceAll("[a-l]", ""))
		// - 15552000000L > 0)
		// conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
		// else {
		conn.setAutoCommit(false);
		conn.setTransactionIsolation(isolation == null
														? Connection.TRANSACTION_READ_UNCOMMITTED
														: isolation);
		// }
		this.conn.set(conn);
	}

	@Override
	public void setPageLenth(Integer len) {
		pageLength = len;
	}

	@Override
	public void setPageNumber(Integer page) {
		pageNumber = page;
	}

	public void setDistinct(boolean distinct) {
		this.distinct = distinct;
	}

	@Override
	public void commit() throws Exception {
		if (conn.get() != null) {
			Connection con = conn.get();
			if (!con.isClosed()) {
				con.commit();
			}
		}
	}

	@Override
	public void rollback() throws Exception {
		if (conn.get() != null) {
			Connection con = conn.get();
			if (!con.isClosed()) {
				con.rollback();
			}
		}
	}

	@Override
	public void close() throws Exception {
		if (conn.get() != null) {
			Connection con = conn.get();
			if (!con.isClosed()) {
				con.close();
			}
			conn.remove();
		}
	}
}
